import { ADD_USER, DELETE_USER, FIND_USER, GET_PAGE_USER } from '@/store/user/CONST'
import { addUserObj, page, userState } from '@/store/user/userIntetFace'
import { Commit } from 'vuex'

import { addUser, deleteUser, getAllUser } from '@/api/user'

export default {
  async [ADD_USER] (context: { commit: Commit }, user: addUserObj) {
    if (!user.username || !user.password) {
      return console.log('添加用户失败:参数错误')
    }

    context.commit(ADD_USER, user)
  },

  async [GET_PAGE_USER] (context: { commit: Commit, state:userState }, page:page = {
    pagesize: context.state.pageSize,
    pagenum: context.state.pageNum
  }) {
    const { data: result } = await getAllUser(page)
    context.commit(GET_PAGE_USER, result)
  },

  [FIND_USER] (context:{commit:Commit}, name:string) {
    if (!name) return console.log('name为空')

    context.commit(FIND_USER, name)
  },

  async [DELETE_USER] (context:{commit:Commit}, id:number) {
    if (!id) return console.warn('id不存在')
    const { meta } = await deleteUser(id)
    console.log(meta.msg)
  }
}
